package com.demo.controller;

import com.demo.common.ResultData;
import com.demo.filter.RequestContextHelper;
import com.demo.sender.sms.AwsSmsSender;
import com.demo.sender.totp.twilio.TwilioTotpSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信
 *
 * @author wangfengchen
 */
@Slf4j
@RestController
@RequestMapping("/sms")
public class SmsController {

  @Autowired
  private AwsSmsSender awsSmsSender;
  @Autowired
  private TwilioTotpSender twilioTotpSender;


  /**
   * 发送短信
   *
   * @return
   */
  @GetMapping("/sendAws")
  public ResultData<Object> sendAws(@RequestParam String phone, @RequestParam String message) {
    return awsSmsSender.send(phone, message);
  }

  @GetMapping("/checkIfPhoneNumberIsOptedOut")
  public ResultData<Object> checkIfPhoneNumberIsOptedOut(@RequestParam String phone) {
    return awsSmsSender.checkIfPhoneNumberIsOptedOut(phone);
  }

  @GetMapping("/listPhoneNumbersOptedOut")
  public ResultData<Object> listPhoneNumbersOptedOut() {
    return awsSmsSender.listPhoneNumbersOptedOut();
  }

  /**
   * 发送验证码
   */
  @GetMapping("/send")
  public ResultData<Object> send(@RequestParam String phone, @RequestParam String countryCode) {
    String requestId = RequestContextHelper.get().getRequestId();
    log.info("requestId = {}，phone = {}，countryCode = {}", requestId, phone, countryCode);
    return twilioTotpSender.send(phone, countryCode);
  }

  /**
   * 校验验证码
   */
  @GetMapping("/verify")
  public ResultData<Object> verify(@RequestParam String phone, @RequestParam String countryCode, @RequestParam String code) {
    String requestId = RequestContextHelper.get().getRequestId();
    log.info("requestId = {}，phone = {}，countryCode = {}，code = {}", requestId, phone, countryCode, code);
    return twilioTotpSender.verify(phone, countryCode, code);
  }

  /**
   * 查询验证状态
   */
  @GetMapping("/getStatus")
  public ResultData<Object> getStatus(@RequestParam String sid) {
    return twilioTotpSender.getStatus(sid);
  }

  /**
   * 修改验证状态
   */
  @GetMapping("/updateStatus")
  public ResultData<Object> updateStatus(@RequestParam String sid) {
    return twilioTotpSender.updateStatus(sid);
  }

  /**
   * 取消验证码
   */
  @GetMapping("/cancelVerify")
  public ResultData<Object> cancelVerify(@RequestParam String sid) {
    return twilioTotpSender.cancelVerify(sid);
  }


}
